<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Home extends \Phpcmf\Home\Module
{

	public function index() {
		$this->_Index();
	}
	//检测帖子是否存在创业金
	public function cunzai(){
		$id = (int)\Phpcmf\Service::L('input')->get('id'); // 帖子id
		$cid = (int)\Phpcmf\Service::L('input')->get('cid'); // 圈子id
		//开始统计
		$huodong_tongji = \Phpcmf\Service::M()->db->table('1_huodong')->where('tieid', $id)->where('quanid', $cid)->where('fstatus',1)->countAllResults();
		$huodong_tongji_data = \Phpcmf\Service::M()->db->table('1_huodong')->where('tieid', $id)->where('quanid', $cid)->where('fstatus',1)->get()->getRowArray();
		//统计所有1_huodong_form_hdbm里面的bmfl值的总和
		$huodong_renshu = \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')
		    ->select('SUM(bmfl) as total_bmfl')
		    ->where('cid', $huodong_tongji_data['id'])
			->where('tieid', $id)
			->where('quanid', $cid)
			->where('shenhe','1')
			->get()
			->getRowArray();
		
		// 总人数为所有bmfl字段值的总和
		$total_renshu = (int)($huodong_renshu['total_bmfl'] ?? 0);
		
		// 同时保留按bmfl字段分组的统计，用于前端展示
		$bmfl_counts = [];
		$grouped_data = \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')
		    ->select('bmfl, COUNT(*) as count')
		    ->where('cid', $huodong_tongji_data['id'])
			->where('tieid', $id)
			->where('quanid', $cid)
			->where('shenhe','1')
			->groupBy('bmfl')
			->get()
			->getResultArray();
		
		foreach ($grouped_data as $item) {
		    $bmfl_counts[$item['bmfl']] = $item['count'];
		}
		if($total_renshu >= $huodong_tongji_data['hdrs']) {
			$yiman = 1; //人数已满
		} else {
			$yiman = 0; //人数未满
		}
		// 检测活动结束时间
		if($huodong_tongji_data['hdjs'] > time() || $yiman ==0){
			$hdjs = 1;
		} else if($huodong_tongji_data['hdjs'] <= time() || $yiman ==1){
			$hdjs = 2;
		} else {
		    $hdjs = 0;
		}
		//还剩余多少人
		$shengyu_renshu = $huodong_tongji_data['hdrs'] - $total_renshu;
		
		// 计算剩余时间
		$shengyu_shijian_9 = $huodong_tongji_data['hdjs'] - time();
		$shengyu_shijian_8 = dr_date($shengyu_shijian_9, 'Y-m-d H:i:s');
		// 确保hdjs字段存在且有效
		$hdjs_time = isset($huodong_tongji_data['hdjs']) && $huodong_tongji_data['hdjs'] ? $huodong_tongji_data['hdjs'] : time();
		$shengyu_shijian_0 = $hdjs_time - time();
		// 确保时间不为负数
		if ($shengyu_shijian_0 < 0) {
		    $shengyu_shijian_0 = 0;
		}
		// 计算剩余天数、小时、分钟
		$days = floor($shengyu_shijian_0 / (24 * 60 * 60));
		$hours = floor(($shengyu_shijian_0 % (24 * 60 * 60)) / (60 * 60));
		$minutes = floor(($shengyu_shijian_0 % (60 * 60)) / 60);
		$seconds = $shengyu_shijian_0 % 60;
		
		// 生成前端需要的JavaScript表达式格式的时间字符串
		// 格式: "1 * 60 * 60 * 1000" 或其他组合
		$shengyu_shijian_expr = '';
		// 确保即使在所有单位为0的情况下也能生成有效的表达式
		if ($shengyu_shijian_0 <= 0) {
		    $shengyu_shijian_expr = '0'; // 当没有剩余时间时，直接设置为0
		} else {
		    if ($days > 0) {
		        $shengyu_shijian_expr .= $days . ' * 24 * 60 * 60';
		    }
		    if ($hours > 0) {
		        $shengyu_shijian_expr .= ($shengyu_shijian_expr ? ' + ' : '') . $hours . ' * 60 * 60';
		    }
		    if ($minutes > 0) {
		        $shengyu_shijian_expr .= ($shengyu_shijian_expr ? ' + ' : '') . $minutes . ' * 60';
		    }
		    if ($seconds > 0 || $shengyu_shijian_expr == '') {
		        $shengyu_shijian_expr .= ($shengyu_shijian_expr ? ' + ' : '') . $seconds;
		    }
		}
		// 最后添加毫秒转换
		$shengyu_shijian_expr .= ' * 1000';
		$hdks = dr_date($huodong_tongji_data['hdks'], 'Y-m-d'); //
		
		// 用户登录的情况下
		//检查是否已经报过名
		$wancheng =0; //进行中
		$baoming =0; //没有报名
		if($this->uid){
		    $huodong_baoming_tongji = \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('tieid', $id)->where('quanid', $cid)->where('buy_uid', $this->uid)->where('shenhe',1)->countAllResults();
		    
		    if($huodong_baoming_tongji > 0){
		        $baoming =1; //已经报名
		    } else {
		        $baoming =0; //没有报名
		    }
		    //确认完成
		    $huodong_wancheng_tongji = \Phpcmf\Service::M()->db->table('1_huodong_form_hdbm')->where('tieid', $id)->where('quanid', $cid)->where('buy_uid', $this->uid)->where('shenhe',1)->where('fufeijifen',100)->countAllResults();
		    if($huodong_wancheng_tongji > 0){
		        $wancheng =1; //已经完成
		    } else {
		        $wancheng =0; //进行中
		    }
		}

		$huodong_tongji_data_0 = [
			'hd_id' => $huodong_tongji_data['id'],
			'title' => $huodong_tongji_data['title'],
			'uid' => $huodong_tongji_data['uid'],
			'hdkg' => $huodong_tongji_data['hdkg'], // 开启1。关闭0
			'hdfw' => $huodong_tongji_data['hdfw'], // 开启1。关闭0
			'hdfs' => $huodong_tongji_data['hdfs'], // 开启1。关闭0
			'baominghedui' => $huodong_tongji_data['baominghedui'], // 开启1。关闭0
			'huodongjifen' => $huodong_tongji_data['huodongjifen'],
			'hdfy' => $huodong_tongji_data['hdfy'], // 开启1。关闭0
			'hdrs' => $huodong_tongji_data['hdrs'], // 人数
			'hdks' => dr_date($huodong_tongji_data['hdks'], 'Y-m-d'),
			'hdjs' => dr_date($huodong_tongji_data['hdjs'], 'Y-m-d'),
			'hdwxh' => $huodong_tongji_data['hdwxh'],
			'hdsjh' => $huodong_tongji_data['hdsjh'],
			'hdsm' => $huodong_tongji_data['hdsm'],
			'hdtc' => $huodong_tongji_data['hdtc'], //退出0
			'quanid' => $huodong_tongji_data['quanid'],
			'tieid' => $huodong_tongji_data['tieid'],
			'hd_js' => $hdjs, // 参与结束时间
			'canjia_renshu' => $total_renshu, //总的参加人数
			'bmfl_counts' => $bmfl_counts, //按bmfl字段分组的人数统计
			'yiman' => $yiman, //0人数未满 。1人数已满
			'shengyu_shijian' => $shengyu_shijian_0, // 剩余时间（秒）
			'shengyu_days' => $days, // 剩余天数
			'shengyu_hours' => $hours, // 剩余小时数
			'shengyu_minutes' => $minutes, // 剩余分钟数
			'shengyu_shijian_expr' => $shengyu_shijian_9, // 前端需要的JavaScript表达式格式的时间字符串 (例如: "1 * 60 * 60 * 1000")
			'shengyu_shijian_8' => $shengyu_shijian_8, // 前端需要的JavaScript表达式格式的时间字符串 (例如: "1 * 60 * 60 * 1000")
			'shengyu_renshu' => $shengyu_renshu, // 剩余人数
			'baoming' => $baoming, //0 没有报名，1是已经报名
			'wancheng' => $wancheng, //0 进行中，1是已经完成
			'sfz' => $huodong_tongji_data['sfz'], // 开启1。关闭0
			'cph' => $huodong_tongji_data['cph'], // 开启1。关闭0
			'xzrs' => $huodong_tongji_data['xzrs'],					
		];
		$this->_json($huodong_tongji, '读取成功', $huodong_tongji_data_0);
	}
	

}
